package com.atguigu.gmall.product.controller;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author mqx
 * @date 2020-8-1 14:15:51
 */
// 测试异步编排
public class TestCompare {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建一个线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                10,
                50,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100)
                // 线程池工厂，拒绝策略
        );
        // 复制小括号，写死右箭头，落地大括号
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> "hello \t");
        // 调用futureA
        // Consumer void accept(T t);
        CompletableFuture<Void> futureB = futureA.thenAcceptAsync((s) -> {
            // 调用睡眠方法
            delaySec(3);
            // 调用一个打印方法
            printCurrTime(s+"这个是线程futureB");
        },threadPoolExecutor);
        // 调用futureA
        CompletableFuture<Void> futureC = futureA.thenAcceptAsync((s) -> {
            // 调用睡眠方法
            delaySec(1);
            // 调用一个打印方法
            printCurrTime(s+"这个是线程futureC");
        },threadPoolExecutor);

        System.out.println(futureB.get());
        System.out.println(futureC.get());
    }

    public static void delaySec(int i){
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void printCurrTime(String str){
        System.out.println(str);
    }
}
